//package com.ucar.train.web;
//
//import com.ucar.train.dto.CrmDTO;
//import com.ucar.train.dto.SysLogDTO;
//import com.ucar.train.service.impl.SysLogServiceImpl;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.util.Date;
//
///**
// * @author wufuqiang
// * @title: InteceptorConfiguration
// * @projectName:train-parent
// * @description: T0D0
// * @created 2019-07-30 13:23
// */
//@Slf4j
//@Component
//public class InteceptorConfiguration implements HandlerInterceptor {
//
//    private Date visitTime;//访问时间
//    private String sysIp;//访问ip地址
//    private String sysUrl;//访问路径（RequestMapping）
//    private String methodName;//方法名
//    private String ClazzName;//类名全路径
//    private String userName;//操作用户名
//
//    @Autowired
//    private SysLogServiceImpl sysLogService;
//
//    @Override
//    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
//        HttpSession session = req.getSession();
//        StringBuffer path = req.getRequestURL();
//
//       // userName= (String) req.getSession().getAttribute("name");
//
//        /**
//         * 拦截器做日志访问
//         *
//         */
//        visitTime = new Date(); //开始时间
//        sysIp = req.getRemoteAddr();
//        sysUrl = path.substring(19);
//        HandlerMethod method = (HandlerMethod) handler;
//        methodName =method.getMethod().getName();
//        Class clazz = method.getBean().getClass();
//        log.info(clazz.getName());
//        ClazzName = clazz.getName().substring(15);
//
//
//
//       return true;
//
//
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//
//        long time = new Date().getTime() - visitTime.getTime(); //获取访问的时长
//
//        //将日志相关信息封装到SysLog对象
//        SysLogDTO sysLog = new SysLogDTO();
//        sysLog.setExecutionTime(time); //执行时长
//        sysLog.setIp(sysIp);
//        sysLog.setMethod("[类名] " + ClazzName + "[方法名] " + methodName);
//        sysLog.setUrl(sysUrl);
//        HttpSession session = request.getSession();
//        CrmDTO crmDTO = (CrmDTO) session.getAttribute("crm");
//        sysLog.setUsername(crmDTO.getUsername());
//        sysLog.setVisitTime(visitTime);
//
//        //调用Service完成操作
//        sysLogService.save(sysLog);
//    }
//}
